package syscommon

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"strconv"
)

type BaseController struct {
	beego.Controller
	//Session session.Session
}

// @Title test
// @Description option处理
// @Success 200 {string} "hello world"
// @router / [options]
//func (c *BaseController) Options() {
//	c.Data["json"] = map[string]interface{}{"status": 200, "message": "ok", "moreinfo": ""}
//	c.ServeJSON()
//}

/*
输入数据json转结构体
 */
func (this *BaseController) Prepare() {
	//session设置
	//添加请求数据
	//this.Inputdata = &this.Ctx.Input.RequestBody
	//
	////添加用户
	//user := this.Ctx.Input.GetData("BaseUser")
	//u, ok := user.(*conf.BaseUser)
	//if ok {
	//	this.User = *u
	//}
	//
	method := this.Ctx.Input.Method()
	if method == "OPTIONS"{
		this.Ctx.Output.SetStatus(200)
		msg,_:=json.Marshal(map[string]interface{}{"status": 200, "message": "ok", "moreinfo": ""})
		this.Ctx.WriteString(string(msg))
		this.StopRun()
		return
	}
}

//json2struct
//func (this *BaseController) ReqJson2Struct(obj interface{}) error {
//	err := json.Unmarshal(*this.Inputdata, obj)
//	return err
//}

//json2map
//func (this *BaseController) ReqJson2map() (map[string]interface{}, error) {
//	var i interface{}
//	err := json.Unmarshal(*this.Inputdata, &i)
//	if err != nil {
//		return nil, err
//	}
//	return i.(map[string]interface{}), nil
//}

//=======================格式化输出================================
/*
json输出
基础
 */
func (this *BaseController) JsonRep(status int, data map[string]interface{}) {
	this.Ctx.Output.SetStatus(status)
	this.Ctx.Output.JSON(data, false, false)
	//this.StopRun()
}

/**
标准化json输出
 */
func (this *BaseController) JsonRepStd(status int, msg string, data interface{}) {
	d := make(map[string]interface{})
	//if status==200{
	//	code=0
	//} else {
	//	code = status
	//}
	d["status"] = status
	d["code"] = status
	d["msg"] = msg
	d["data"] = data
	this.JsonRep(status, d)
}

/**
分页标准化输出
 */
func (this *BaseController) JsonReqStdS(status, count, limit, page int, data interface{}) {
	d := make(map[string]interface{})
	code:=0
	if status == 200{
		code =0
	}else{
		code = status
	}
	d["code"] = code
	d["status"] = status
	d["count"] = count
	d["limit"] = limit
	d["page"] = page
	d["data"] = data
	this.JsonRep(status, d)
}

/**
200json
 */
func (this *BaseController) JsonRep200(msg string) {
	this.JsonRepStd(200, msg, nil)
}
func (this *BaseController) JsonRep200x(msg string, data interface{}) {
	this.JsonRepStd(200, msg, data)
}

/**
500Json
 */
func (this *BaseController) JsonRep500(msg string) {
	this.JsonRepStd(500, msg, nil)
}

//json400
func (this *BaseController) JsonRep400(msg string) {
	this.JsonRepStd(400, msg, nil)
}

func (this *BaseController) JsonRep500x(msg string, data interface{}) {
	this.JsonRepStd(500, msg, data)
}

//删除
func (this *BaseController) Ok204() {
	this.Ctx.Output.SetStatus(204)
	this.Ctx.Output.Body([]byte{})
}

//403

func (this *BaseController) Json403(msg string, data interface{}) {
	this.Ctx.Output.SetStatus(403)
	d := make(map[string]interface{})
	d["code"] = 403
	d["message"] = "Forbidden"
	d["data"] = data
	d["status"] = 403
	this.Ctx.Output.JSON(d, false, false)
}

//==================================================================
/*
参数验证
字符验证 并且转换为int
 */
func (this *BaseController) S2i(s string, num int) (t int) {
	if s == "" {
		return num
	}
	t, _ = strconv.Atoi(s)
	return t
}

//============================================
//json反序列化成map

